using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using backend.Data;
using backend.Models;
using backend.Models.Dtos;
using Microsoft.EntityFrameworkCore;

namespace backend.Repositories
{
    public class StatsRepo
    {
        private readonly CampusFacilityRepairManagementDbContext _context;
        public StatsRepo(CampusFacilityRepairManagementDbContext context)
        {
            _context = context;
        }

        public async Task<List<AreaStatDto>> GetFailureAreaStatsAsync(
        int startYear,
        int startMonth,
        int? endYear,
        int? endMonth)
        {
            return await _context.FailureAreaStatistics
                .Where(fas => fas.FdYear >= startYear && fas.FdYear <= endYear &&
                              fas.FdMonth >= startMonth && fas.FdMonth <= endMonth)
                .Select(fas => new AreaStatDto {
                    AreaName = _context.FirstClasses.Where(fc => fc.FcId == fas.FcId).Select(fc => fc.FcName).First(),
                    Number = fas.TotalTimes
                })
                .ToListAsync();
        }

        public async Task<List<FaultTypeStatDto>> GetFailureTypeStatsAsync(
            int startYear,
            int startMonth,
            int? endYear,
            int? endMonth)
        {
            return await _context.FailureTypeStatistics
                .Where(fas => fas.FdYear >= startYear && fas.FdYear <= endYear &&
                              fas.FdMonth >= startMonth && fas.FdMonth <= endMonth)
                .Select(fas => new FaultTypeStatDto {
                    FaultType = _context.Failures.Where(f => f.FId == fas.FId).Select(f => f.FName).First(),
                    Number = fas.TotalTimes
                }) 
                .ToListAsync();
        }

        public async Task<RatingStatDto> GetRatingStatAsync(int startYear,
            int startMonth,
            int? endYear,
            int? endMonth) 
        {
            var repairData = await _context.RepairData
            .Where(rd => rd.RdYear >= startYear && rd.RdYear <= endYear &&
                         rd.RdMonth >= startMonth && rd.RdMonth <= endMonth)
            .ToListAsync();

             if (!repairData.Any())
            {
                return new RatingStatDto
                {
                    TotalAverage = 0,
                    ResponseTime = 0,
                    ServiceQuality = 0,
                    ServiceAttitude = 0
                };
            }

            var responseTime = repairData.Sum(rd => rd.RdTotalResponseTime) / repairData.Count;
            var serviceQuality = repairData.Sum(rd => rd.RdTotalQuality) / repairData.Count;
            var serviceAttitude = repairData.Sum(rd => rd.RdTotalAttitude) / repairData.Count;

            // 计算总平均值
            var totalAverage = (responseTime + serviceQuality + serviceAttitude) / 3;

            return new RatingStatDto
            {
                TotalAverage = totalAverage,
                ResponseTime = responseTime,
                ServiceQuality = serviceQuality,
                ServiceAttitude = serviceAttitude
            };
        }
    }
}